package com.cgw.room.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.cgw.room.MainApp;


/**
 * Created by li_bin on 2016/10/28.
 * SharedPreferences存储帮助类
 */
public class SharedPreferencesUtil {

    private final static String LOTTERY_PREF = "jcecg_pref";

    private static SharedPreferences getSharedPreferences() {
        SharedPreferences setting = MainApp.getInstance().getSharedPreferences(LOTTERY_PREF, Context.MODE_PRIVATE);
        return setting;
    }

    /**
     * 保存String类型值
     *
     * @param key
     * @param value
     */
    public static void setStringValue(String key, String value) {
        SharedPreferences sp = getSharedPreferences();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 获取String类型的Value值
     *
     * @param key          Map->key
     * @param defaultValue 若没值的情况下的默认值
     * @return
     */
    public static String getStringValue(String key, String defaultValue) {
        SharedPreferences settings = getSharedPreferences();
        return settings.getString(key, defaultValue);
    }

    /**
     * 保存Int类型的值
     *
     * @param key
     * @param value
     */
    public static void setIntValue(String key, int value) {
        SharedPreferences sp = getSharedPreferences();
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取Int类型的值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getIntValue(String key, int defaultValue) {
        SharedPreferences settings = getSharedPreferences();
        return settings.getInt(key, defaultValue);
    }

    /**
     * 保存Boolean类型值
     *
     * @param key
     * @param value
     */
    public static void setBooleanValue(String key, boolean value) {
        SharedPreferences sp = getSharedPreferences();
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 获取Boolean类型值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBooleanValue(String key, boolean defaultValue) {
        SharedPreferences settings = getSharedPreferences();
        return settings.getBoolean(key, defaultValue);
    }

    public static void setLongValue(String key, long value) {
        SharedPreferences sp = getSharedPreferences();
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLongValue(String key, long defaultValue) {
        SharedPreferences settings = getSharedPreferences();
        return settings.getLong(key, defaultValue);
    }

    /**
     * 清除sp key
     *
     * @param key
     */
    public static void clearSpValue(String key) {
        SharedPreferences sp = getSharedPreferences();
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }
}
